//
//  HandwritingSwiftPlugins.swift
//  handwritingprocessing
//
//  Created by Wdc on 2020/7/2.
//

import Flutter
import UIKit

enum FlutterError: Error {
    case runtimeError(String)
}

public class HandwritingSwiftPlugins: NSObject, FlutterPlugin {
    static let noArgumentsWasPassesError = "no arguments was passed"
    static let wasNotAbleParsImage = "was not able parse image"

  public static func register(with registrar: FlutterPluginRegistrar) {
    let channel = FlutterMethodChannel(name: "handwritingprocessing", binaryMessenger: registrar.messenger())
    let instance = HandwritingSwiftPlugins()
    registrar.addMethodCallDelegate(instance, channel: channel)
  }
    
  public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
    do {
        try next(call, result: result)
    }catch {
        result(FlutterMethodNotImplemented)
    }
  }
    
    func next(_ call: FlutterMethodCall, result: @escaping FlutterResult) throws{
        if "handwriting" == call.method {
          try  handwriting(call, result: result)
        }else{
            result(FlutterMethodNotImplemented)
        }
       }
    
    func handwriting(_ call: FlutterMethodCall, result: @escaping FlutterResult) throws{
            guard let dictionary = call.arguments as? NSDictionary else{
                throw FlutterError.runtimeError(HandwritingSwiftPlugins.noArgumentsWasPassesError)
            }
            
            guard let byteData = dictionary["byteData"] as? FlutterStandardTypedData,
            let type = dictionary["type"] as? Int32,
            let blackWriteVal = dictionary["blackWriteVal"] as? Int32,
            let removalRedVal = dictionary["removalRedVal"] as? Int32,
            let removalBlueVal = dictionary["removalBlueVal"] as? Int32,
            let patchSize = dictionary["patchSize"] as? Int32
                  else{
                throw FlutterError.runtimeError(HandwritingSwiftPlugins.noArgumentsWasPassesError)
            }
            
            if let firstImage = UIImage(data:byteData.data,scale:1.0){
                let outIamge = HandwritingOpencv.handwriting(firstImage, type: type,blackWriteVal: blackWriteVal,removalRedVal: removalRedVal,removalBlueVal: removalBlueVal,patchSize:patchSize)
//              let imageData = UIImagePNGRepresentation(outIamge)
                let imageData = outIamge.pngData()
                
//                let url = URL(string: "https://csdnimg.cn/release/ask/assets/biwen_right-7530c01f86094c60eff8a43492efa8ef.png")
//                do {
//                    let imageData = try Data(contentsOf: url!)
//                    let image = UIImage(data: imageData)
//                    result( ["results":  image?.pngData()] )
//                }catch let error as NSError {
//                    print(error)
//                }
                
//                let data=FlutterStandardTypedData(bytes: imageData!)
//                  result( ["results":  imageData] )
                  result( imageData )
            }else{
              throw FlutterError.runtimeError(HandwritingSwiftPlugins.wasNotAbleParsImage)
            }
          }
}
